/**********************************************************************************************
Do-file:		Donor responses.do
Date:			28th July 2020
Author:			Jennifer Mayo
***********************************************************************************************/

clear
set more off

global name "jmp4k"
global home "/Users/$name/Library/CloudStorage/OneDrive-UniversityofMissouri/Projects/Navigating the notches"
global data "$home/Replication"
global output "$home/CN Output"

use "$data/FinancialMetrics.dta", clear

set scheme s1mono

***********************************************************************************************
********************************************* DATA PREP ***************************************
***********************************************************************************************

merge m:m ein ratingid using "$data/Form990objects.dta", keepusing(administrativeexpenses fundraisingexpenses programexpenses totalexpenses totalcontributions totalnetassets)
drop if _merge==2
drop _merge  

merge m:m ein ratingid using "$data/Fin and A&T scores.dta", keepusing(finscore accountscore)
drop if _merge==2
drop _merge  

drop justone
duplicates drop

rename PublicationYear year
merge m:m year using "$data/CPI.dta", keepusing(annualcpi)
drop if _merge==2
drop _merge 
rename year PublicationYear

* Generate star variable
gen star = 0 if score<55
replace star = 1 if score>=55 & score<70
replace star = 2 if score>=70 & score<80
replace star = 3 if score>=80 & score<90
replace star = 4 if score>=90
label var star "Star rating"

* Drop if no expenses
drop if totalexpenses==.
sort ein PublicationYear PublicationMonth
by ein: gen temp = totalexpenses - totalexpenses[_n-1]  // charities whose financials didn't change but their score/star rating did (temp = 0 means no change in expenses)


bys ein: gen temp_star2 = star - star[_n-1]  if PublicationYear==2016 & PublicationMonth>=6 & temp==0 // identify change in star for charities whose financials didn't change 
label var temp_star2 "Change in star rating for charities whose financials didn't change"

bys ein: gen rescored = temp_star2 - temp_star2[_n-1]   // some charities are rescored more than once in 2016 (without financials changing)

bys ein: replace temp_star2=. if rescored==0
replace temp_star2 = . if rescored==-1   // rescored=-1 if rating increase in Month 6 and then got rescored later in the year but stayed at the month 6 rating
bys ein: replace temp_star2 = temp_star2[_n+1] if temp_star2==0 & temp_star2[_n+1]==2   // rescored=2 if first re-rating didn't change star but then second re-rating increased star
bys ein: replace temp_star2 = temp_star2[_n+1] if temp_star2==-1 & temp_star2[_n+1]==1   // rescored=2 if first re-rating reduced star but then second re-rating increased star back up again

bys ein: replace temp_star2 = . if temp_star2==0 & temp_star2[_n-1]==-1   
bys ein: replace temp_star2 = temp_star2[_n+1] if temp_star2[_n+1]==1 & temp_star2==0   

replace temp_star2=2 if rescored==-2 & ein==10651843
replace temp_star2=. if rescored==-2 & ein==412075995
replace temp_star2=0 if ein==412075995
replace temp_star2=. if rescored==-2 & ein==916057907

bys ein: egen methodstarchange = max(temp_star2)   // methodstarchange!=. means we can observe change in score from change in methodology - only use this sub-sample
replace temp_star2=0 if temp_star2==. & methodstarchange!=.
drop rescored


gen ln_totalcontributions = log(totalcontributions)
bys ein PublicationYear PublicationMonth: gen Temp = _N   
drop if Temp>1 & fundraisingexpensesratio==.
drop Temp

gen modate = ym(PublicationYear,PublicationMonth)
label var modate "Date that combines publication month and year"
xtset ein modate
sort ein modate

// Generate dummy equal to 1 when CN 2.1 is in place (and charity has a new rating)
bys ein: egen postchange = max(temp_star2)
bys ein: egen losestar = min(temp_star2)
replace postchange = 1 if losestar<0
replace postchange = 0 if PublicationYear<2016
replace postchange = 0 if PublicationYear==2016 & PublicationMonth<6
label var postchange "CN 2.1 in place and charity has new rating"

bys ein: gen newrating_L1 = postchange[_n-1]
bys ein: egen treated = max(newrating_L1)


// Generate dummy for when CN 2.1 is in place (regardless of whether charity has a new rating)
gen CN2_1 = postchange
replace CN2_1 = 1 if PublicationYear==2016 & PublicationMonth>=6 & newrating_L1==0
replace CN2_1 = 1 if PublicationYear>2016 & newrating_L1==0

label var CN2_1 "CN 2.1 in place"

bys ein: gen previousstar = star[_n-1] if  PublicationYear==2016 & PublicationMonth>=6 & temp==0 
bys ein: egen old_star = min(previousstar)
label var old_star "Star rating period before CN 2.1"
drop previousstar

bys ein: gen L1_fundraisingefficiency = fundraisingefficiency[_n-1]  // fundraising efficiency is fundraising/contributions
label var L1_fundraisingefficiency "Fundraising efficiency in t-1"
gen L1_price = 1/(1-L1_fundraisingefficiency)

gen star_at_change = star if temp==0 & PublicationYear==2016 & PublicationMonth>=6
bys ein: egen staratchange = max(star_at_change)
label var staratchange "Star awarded by CN 2.1"
drop star_at_change

bys ein: gen fundraisingexp_L1 = fundraisingexpenses[_n-1]/1000000
label var fundraisingexp_L1 "Fundraising expenses ($million) in t-1"
bys ein: gen netassets_L1 = totalnetassets[_n-1]/1000000
label var netassets_L1 "Net assets ($million) in t-1"


**** Deflate everything (make 2010 the base year)
gen cpitemp = annualcpi if PublicationYear==2010
egen cpitemp2 = max(cpitemp)

gen cpideflate = annualcpi/cpitemp2

** Multiply everything by cpideflate
gen totalcontributions_deflated = totalcontributions*cpideflate
gen ln_totalcontributions_deflate = log(totalcontributions_deflated) if totalcontributions_deflated>=0
gen fundraisingexp_L1_deflated = fundraisingexp_L1*cpideflate
gen netassets_L1_deflated = netassets_L1*cpideflate
gen netassets_L1_deflated_thousands = netassets_L1_deflated*1000
gen totalnetassets_deflated = totalnetassets*cpideflate
gen fundraisingexpenses_deflated = fundraisingexpenses*cpideflate
gen programexpenses_deflated = programexpenses*cpideflate
gen ln_programexpenses_deflated = log(programexpenses_deflated) if programexpenses_deflated>=0
gen ln_fundraisingexpenses_deflated = log(fundraisingexpenses_deflated) if fundraisingexpenses_deflated>=0


************************************************************************************************
****************************************** DONATION FIGURES ************************************
************************************************************************************************

********************************
** TABLE 3: Transition matrix **
********************************

gen aaa=star if PublicationYear==2015
by ein: egen star2015=max(aaa)
tab star if star2015==0 &  PublicationYear==2016 &  PublicationMonth>=6
tab star if star2015==1 &  PublicationYear==2016 &  PublicationMonth>=6
tab star if star2015==2 &  PublicationYear==2016 &  PublicationMonth>=6
tab star if star2015==3 &  PublicationYear==2016 &  PublicationMonth>=6
tab star if star2015==4 &  PublicationYear==2016 &  PublicationMonth>=6


**************************************************
**** APPENDIX FIGURE D5: Bunching from Above  ****
**************************************************

gen above90 = 1 if score>=90 & PublicationYear!=2016 & PublicationYear!=2011
bys ein: egen above90_total = sum(above90) if PublicationYear!=2016 & PublicationYear!=2011
bys ein: gen yearsinsample = _N if PublicationYear!=2016 & PublicationYear!=2011
gen propabove90 = above90_total/yearsinsample
bysort ein (propabove90): replace propabove90 = propabove90[1] if missing(propabove90)
bys PublicationYear: egen meanscore = mean(score) if propabove90>0.80 
bys PublicationYear: egen stdscore = sd(score) if propabove90>0.80 

serrbar meanscore stdscore PublicationYear if propabove90>0.8, graphregion(color(white)) xtitle("Publication Year") ytitle("Mean score") lwidth(thin) lpattern(dash) mvopts(msize(small)) note("Notes: The sample is restricted to all charities scoring above 90 more than 80% of the time" "            The dashed vertical lines represent standard deviations")

graph export "$output/FigureD5.eps", replace



***********************************************************
** APPENDIX FIGURE D.6: Stability of a ratings downgrade **
***********************************************************

gen CN21worse=1 if staratchange<old_star
bys PublicationYear: egen meanscore0 = mean(score) if PublicationYear>2016 & staratchange==0 &  CN21worse==1
bys PublicationYear: egen meanscore1 = mean(score) if PublicationYear>2016 & staratchange==1 &  CN21worse==1
bys PublicationYear: egen meanscore2 = mean(score) if PublicationYear>2016 & staratchange==2 &  CN21worse==1
bys PublicationYear: egen meanscore3 = mean(score) if PublicationYear>2016 & staratchange==3 &  CN21worse==1
bys PublicationYear: egen stdscore0 = sd(score) if PublicationYear>2016 & staratchange==0 &  CN21worse==1
bys PublicationYear: egen stdscore1 = sd(score) if PublicationYear>2016 & staratchange==1 &  CN21worse==1
bys PublicationYear: egen stdscore2 = sd(score) if PublicationYear>2016 & staratchange==2 &  CN21worse==1
bys PublicationYear: egen stdscore3 = sd(score) if PublicationYear>2016 & staratchange==3 &  CN21worse==1


bys PublicationYear: egen meanstar0 = mean(star) if PublicationYear>2016 & staratchange==0 &  CN21worse==1
bys PublicationYear: egen meanstar1 = mean(star) if PublicationYear>2016 & staratchange==1 &  CN21worse==1
bys PublicationYear: egen meanstar2 = mean(star) if PublicationYear>2016 & staratchange==2 &  CN21worse==1
bys PublicationYear: egen meanstar3 = mean(star) if PublicationYear>2016 & staratchange==3 &  CN21worse==1
bys PublicationYear: egen stdstar0 = sd(star) if PublicationYear>2016 & staratchange==0 &  CN21worse==1
bys PublicationYear: egen stdstar1 = sd(star) if PublicationYear>2016 & staratchange==1 &  CN21worse==1
bys PublicationYear: egen stdstar2 = sd(star) if PublicationYear>2016 & staratchange==2 &  CN21worse==1
bys PublicationYear: egen stdstar3 = sd(star) if PublicationYear>2016 & staratchange==3 &  CN21worse==1

gen halfstdstar0 = stdstar0/2
gen halfstdstar1 = stdstar1/2
gen halfstdstar2 = stdstar2/2
gen halfstdstar3 = stdstar3/2

gen upper0 = meanstar0 + halfstdstar0
gen lower0 = meanstar0 - halfstdstar0
gen upper1 = meanstar1 + halfstdstar1
gen lower1 = meanstar1 - halfstdstar1
gen upper2 = meanstar2 + halfstdstar2
gen lower2 = meanstar2 - halfstdstar2
gen upper3 = meanstar3 + halfstdstar3
gen lower3 = meanstar3 - halfstdstar3

twoway rcap upper0 lower0 PublicationYear if PublicationYear>2016 /// 
||     rcap upper1 lower1 PublicationYear if PublicationYear>2016 /// 
||     rcap upper2 lower2 PublicationYear if PublicationYear>2016 /// 
||     rcap upper3 lower3 PublicationYear if PublicationYear>2016 /// 
||     scatter meanstar0 PublicationYear if PublicationYear>2016, msymbol(Dh) /// 
||     scatter meanstar1 PublicationYear if PublicationYear>2016, msymbol(Dh) ///
||     scatter meanstar2 PublicationYear if PublicationYear>2016, msymbol(Dh) ///
||     scatter meanstar3 PublicationYear if PublicationYear>2016, msymbol(Dh) ///
ytitle("Mean star") legend(order(1 "Downgraded to 0 star" 2 "Downgraded to 1 star" 3 "Downgraded to 2 star" 4 "Downgraded to 3 star")) ///
xlabel(2017(1)2019) ylabel(0(1)4) xtitle("Publication Year") graphregion(color(white)) lwidth(thin) lpattern(dash) ///
note("Note: The sample is restricted to organizations whose star rating declines under CN 2.1")

graph export "$output/FigureD6.eps", replace

***********************************************************************************************
*** DONATION REGRESSIONS
***********************************************************************************************

***************** Change in donations following change in ratings *****************

* Dependent var:
sort ein modate 
by ein: gen L1_totalcontdeflate = totalcontributions_deflated[_n-1]  
gen realtotalcontchange = totalcontributions_deflated - L1_totalcontdeflate
gen percentchange_totalcont = realtotalcontchange/L1_totalcontdeflate

** Binary indicators for star change
gen three_to_four = 1 if methodstarchange!=. & (staratchange==4 & old_star==3)
replace three_to_four=0 if methodstarchange!=. & three_to_four==.
gen two_to_three = 1 if methodstarchange!=. & (staratchange==3 & old_star==2)
replace two_to_three=0 if methodstarchange!=. & two_to_three==.
gen one_to_two = 1 if methodstarchange!=. & (staratchange==2 & old_star==1)
replace one_to_two=0 if methodstarchange!=. & one_to_two==.
gen zero_to_one = 1 if methodstarchange!=. & (staratchange==1 & old_star==0)
replace zero_to_one=0 if methodstarchange!=. & zero_to_one==.

gen four_to_three = 1 if methodstarchange!=. & (staratchange==3 & old_star==4)
replace four_to_three=0 if methodstarchange!=. & four_to_three==.
gen three_to_two = 1 if methodstarchange!=. & (staratchange==2 & old_star==3)
replace three_to_two=0 if methodstarchange!=. & three_to_two==.
gen two_to_one = 1 if methodstarchange!=. & (staratchange==1 & old_star==2)
replace two_to_one=0 if methodstarchange!=. & two_to_one==.
gen one_to_zero = 1 if methodstarchange!=. & (staratchange==0 & old_star==1)
replace one_to_zero=0 if methodstarchange!=. & one_to_zero==.

** Controls
sort ein modate
by ein: gen fundexp_deflated_L1 = fundraisingexpenses_deflated[_n-1]
by ein: gen fundexp_deflated_L2 = fundraisingexpenses_deflated[_n-2]
by ein: gen fundexp_deflated_L3 = fundraisingexpenses_deflated[_n-3]
gen fundexp_change_lagged = fundexp_deflated_L2-fundexp_deflated_L3

by ein: gen netassets_deflated_L2 = netassets_L1_deflated[_n-1]
by ein: gen netassets_deflated_L3 = netassets_L1_deflated[_n-2]
gen netassets_change_lagged = netassets_deflated_L2-netassets_deflated_L3

gen fundeff = fundraisingexpenses/totalcontributions
by ein: gen fundeff_L1 = fundeff[_n-1]
by ein: gen fundeff_L2 = fundeff[_n-2]
by ein: gen fundeff_L3 = fundeff[_n-3]
gen fundeff_change_lagged = fundeff_L2-fundeff_L3


sort ein modate 
by ein: gen CN2_1L1 = CN2_1[_n-1]
by ein: gen first_obs_after_change = CN2_1L1 - CN2_1L1[_n-1]



***********************************************************************
** TABLE 4: Effect on Donations of a Change in Star Rating - 2015-18 **
***********************************************************************

replace three_to_four=0 if methodstarchange!=. & newrating_L1==0 
replace three_to_four=. if methodstarchange!=. & newrating_L1==.
replace two_to_three=0 if methodstarchange!=. & newrating_L1==0 
replace two_to_three=. if methodstarchange!=. & newrating_L1==.
replace one_to_two=0 if methodstarchange!=. & newrating_L1==0 
replace one_to_two=. if methodstarchange!=. & newrating_L1==.
replace zero_to_one=0 if methodstarchange!=. & newrating_L1==0 
replace zero_to_one=. if methodstarchange!=. & newrating_L1==.


bys ein: egen ratingdowngrade = min(temp_star2)
replace ratingdowngrade = 0 if PublicationYear<2016
replace ratingdowngrade = 0 if PublicationYear==2016 & PublicationMonth<6
label var ratingdowngrade "CN 2.1 in place"

bys ein: gen downgrade_L1 = ratingdowngrade[_n-1]

replace four_to_three=0 if methodstarchange!=. & downgrade_L1==0
replace four_to_three=. if methodstarchange!=. & downgrade_L1==.
replace three_to_two=0 if methodstarchange!=. & downgrade_L1==0 
replace three_to_two=. if methodstarchange!=. & downgrade_L1==.
replace two_to_one=0 if methodstarchange!=. & downgrade_L1==0 
replace two_to_one=. if methodstarchange!=. & downgrade_L1==.
replace one_to_zero=0 if methodstarchange!=. & downgrade_L1==0 
replace one_to_zero=. if methodstarchange!=. & downgrade_L1==.

eststo fe_reg: xtreg ln_totalcontributions_deflate three_to_four two_to_three one_to_two zero_to_one four_to_three three_to_two two_to_one one_to_zero fundraisingexp_L1_deflated netassets_L1_deflated L1_fundraisingefficiency CN2_1  i.modate if PublicationYear>=2015 & PublicationYear<=2018 & methodstarchange!=., fe vce(cluster ein)

gen donationsregsample = e(sample)
gen totalnetassetsdef_mill = totalnetassets/1000000
sum totalnetassetsdef_mill if donationsregsample==1, det // 10th percentile = 0.707822; 90th = 46.25244; 50% = 5.360655 


eststo fe_reg_small: xtreg ln_totalcontributions_deflate three_to_four two_to_three one_to_two zero_to_one four_to_three three_to_two two_to_one one_to_zero fundraisingexp_L1_deflated netassets_L1_deflated L1_fundraisingefficiency CN2_1  i.modate if PublicationYear>=2015 & PublicationYear<=2018 & methodstarchange!=. & totalnetassetsdef_mill<5.360655, fe vce(cluster ein)

eststo fe_reg_large: xtreg ln_totalcontributions_deflate three_to_four two_to_three one_to_two zero_to_one four_to_three three_to_two two_to_one one_to_zero fundraisingexp_L1_deflated netassets_L1_deflated L1_fundraisingefficiency CN2_1  i.modate if PublicationYear>=2015 & PublicationYear<=2018 & methodstarchange!=. & totalnetassetsdef_mill>5.360655, fe vce(cluster ein)


*** LaTeX table

estadd local charity_fixed "Yes", replace : *
estadd local time_fixed "Yes", replace : *


esttab fe_reg fe_reg_small fe_reg_large  ///
  using "$output/donor_response_fe.tex", ///
  replace label nogap nonote ///
  se ///
  mlabels("\shortstack{All charities}" "\shortstack{Small}" "\shortstack{Large}") ///
  keep(three_to_four two_to_three one_to_two zero_to_one four_to_three three_to_two two_to_one one_to_zero) ///
  order(three_to_four two_to_three one_to_two zero_to_one four_to_three three_to_two two_to_one one_to_zero) ///
  varlabels( ///
            three_to_four "Rating increased from 3 to 4 stars" ///
            two_to_three "Rating increased from 2 to 3 stars" ///
            one_to_two "Rating increased from 1 to 2 stars" ///
            zero_to_one "Rating increased from 0 to 1 star" ///
            four_to_three "Rating decreased from 4 to 3 stars" ///
            three_to_two "Rating decreased from 3 to 2 stars" ///
            two_to_one "Rating decreased from 2 to 1 star" ///
            one_to_zero "Rating decreased from 1 to 0 stars" ///
            ) ///
  nonumbers ///
  eqlabels(none) ///
  collabels(none) ///
  star(* 0.1 ** 0.05 *** 0.01) ///
  legend ///
  addnotes("Notes: The dependent variable is log(contributions)" "Regressions control for fundraising expenses, net assets, and fundraising efficiency - all lagged by one period" "Small (large) charities are those with net assets less (greater) than $5.6 million (50th percentile)" "Standard errors (in parentheses) clustered at the charity level") ///
  stats(charity_fixed time_fixed N r2, fmt(%9.0g) ///
        labels("Charity Fixed Effects" ///
               "Time Fixed Effects" ///
				"Observations" ///
               "R-Squared" ///
               ) ///
       ) ///
  title("Effect on Donations of a Change in Star Rating")
  


***************************************************************************************
** TABLE C.3: Effect on Donations of a Change in Star Rating (No Controls) - 2015-18 **
***************************************************************************************

eststo fe_reg: xtreg ln_totalcontributions_deflate three_to_four two_to_three one_to_two zero_to_one four_to_three three_to_two two_to_one one_to_zero CN2_1  i.modate if PublicationYear>=2015 & PublicationYear<=2018 & methodstarchange!=., fe vce(cluster ein)

gen donationsregsample2 = e(sample)
sum totalnetassetsdef_mill if donationsregsample2==1, det // 10th percentile = 0.707822; 90th = 46.25244; 50% = 5.360655 


eststo fe_reg_small: xtreg ln_totalcontributions_deflate three_to_four two_to_three one_to_two zero_to_one four_to_three three_to_two two_to_one one_to_zero CN2_1  i.modate if PublicationYear>=2015 & PublicationYear<=2018 & methodstarchange!=. & totalnetassetsdef_mill<5.360655, fe vce(cluster ein)

eststo fe_reg_large: xtreg ln_totalcontributions_deflate three_to_four two_to_three one_to_two zero_to_one four_to_three three_to_two two_to_one one_to_zero  CN2_1  i.modate if PublicationYear>=2015 & PublicationYear<=2018 & methodstarchange!=. & totalnetassetsdef_mill>5.360655, fe vce(cluster ein)


*** LaTeX table

estadd local charity_fixed "Yes", replace : *
estadd local time_fixed "Yes", replace : *


esttab fe_reg fe_reg_small fe_reg_large  ///
  using "$output/donor_response_fenocontrol.tex", ///
  replace label nogap nonote ///
  se ///
  mlabels("\shortstack{All charities}" "\shortstack{Small}" "\shortstack{Large}") ///
  keep(three_to_four two_to_three one_to_two zero_to_one four_to_three three_to_two two_to_one one_to_zero) ///
  order(three_to_four two_to_three one_to_two zero_to_one four_to_three three_to_two two_to_one one_to_zero) ///
  varlabels( ///
            three_to_four "Rating increased from 3 to 4 stars" ///
            two_to_three "Rating increased from 2 to 3 stars" ///
            one_to_two "Rating increased from 1 to 2 stars" ///
            zero_to_one "Rating increased from 0 to 1 star" ///
            four_to_three "Rating decreased from 4 to 3 stars" ///
            three_to_two "Rating decreased from 3 to 2 stars" ///
            two_to_one "Rating decreased from 2 to 1 star" ///
            one_to_zero "Rating decreased from 1 to 0 stars" ///
            ) ///
  nonumbers ///
  eqlabels(none) ///
  collabels(none) ///
  star(* 0.1 ** 0.05 *** 0.01) ///
  legend ///
  addnotes("Notes: The dependent variable is log(contributions)" "Regressions control for fundraising expenses, net assets, and fundraising efficiency - all lagged by one period" "Small (large) charities are those with net assets less (greater) than $5.4 million (50th percentile)" "Standard errors (in parentheses) clustered at the charity level") ///
  stats(charity_fixed time_fixed N r2, fmt(%9.0g) ///
        labels("Charity Fixed Effects" ///
               "Time Fixed Effects" ///
				"Observations" ///
               "R-Squared" ///
               ) ///
       ) ///
  title("Effect on Donations of a Change in Star Rating")  
  
  
  
  
************************************************************************************
** TABLE C.4: Effect on Donations of a Change in Star Rating - Immediate Response **
************************************************************************************

eststo fd_reg: reg percentchange_totalcont three_to_four two_to_three one_to_two zero_to_one four_to_three three_to_two two_to_one one_to_zero fundexp_change_lagged netassets_change_lagged fundeff_change_lagged if methodstarchange!=. & first_obs_after_change==1, vce(cluster ein)

gen donationsregsample3 = e(sample)
sum totalnetassetsdef_mill if donationsregsample3==1, det // 10th percentile = 0.739074; 90th = 48.3309; 50% = 5.590635 

eststo fd_reg_small: reg percentchange_totalcont three_to_four two_to_three one_to_two zero_to_one four_to_three three_to_two two_to_one one_to_zero fundexp_change_lagged netassets_change_lagged fundeff_change_lagged if methodstarchange!=. & first_obs_after_change==1 & totalnetassetsdef_mill<5.590635 , vce(cluster ein)


eststo fd_reg_large: reg percentchange_totalcont three_to_four two_to_three one_to_two zero_to_one four_to_three three_to_two two_to_one one_to_zero fundexp_change_lagged netassets_change_lagged fundeff_change_lagged if methodstarchange!=. & first_obs_after_change==1 & totalnetassetsdef_mill>5.590635 , vce(cluster ein)


*** LaTeX table

estadd local charity_fixed "Yes", replace : *
estadd local time_fixed "Yes", replace : *


esttab fd_reg fd_reg_small fd_reg_large  ///
  using "$output/donor_response_fd.tex", ///
  replace label nogap nonote ///
  se ///
  mlabels("\shortstack{All charities}" "\shortstack{Small}" "\shortstack{Large}") ///
  keep(three_to_four two_to_three one_to_two zero_to_one four_to_three three_to_two two_to_one one_to_zero) ///
  order(three_to_four two_to_three one_to_two zero_to_one four_to_three three_to_two two_to_one one_to_zero) ///
  varlabels( ///
            three_to_four "Rating increased from 3 to 4 stars" ///
            two_to_three "Rating increased from 2 to 3 stars" ///
            one_to_two "Rating increased from 1 to 2 stars" ///
            zero_to_one "Rating increased from 0 to 1 star" ///
            four_to_three "Rating decreased from 4 to 3 stars" ///
            three_to_two "Rating decreased from 3 to 2 stars" ///
            two_to_one "Rating decreased from 2 to 1 star" ///
            one_to_zero "Rating decreased from 1 to 0 stars" ///
            ) ///
  nonumbers ///
  eqlabels(none) ///
  collabels(none) ///
  star(* 0.1 ** 0.05 *** 0.01) ///
  legend ///
  addnotes("Notes: The dependent variable is the percent change in total contributions from June 1st 2016 to the subsequent rating" "Regressions control for changes in fundraising expenses, net assets, and fundraising efficiency - all lagged by one period" "Small (large) charities are those with net assets less (greater) than $5.6 million (50th percentile)" "Standard errors (in parentheses) clustered at the charity level") ///
  stats(charity_fixed time_fixed N r2, fmt(%9.0g) ///
        labels("Charity Fixed Effects" ///
               "Time Fixed Effects" ///
				"Observations" ///
               "R-Squared" ///
               ) ///
       ) ///
  title("Effect on Donations of a Change in Star Rating - Immediate Response")
    



********************************************
** TABLE C.5: IV reduced form (1) 2015-18 **
********************************************

sort ein modate
drop aaa
bys ein CN2_1: gen aaa = _n  
gen starivtemp = star if postchange==1 & aaa==1   //aaa==1 is the immediate post-period
sort ein modate
by ein: carryforward starivtemp, gen(star_iv)   // fix the rating received in immediate post-period
replace star_iv = star if star_iv==.
drop starivtemp aaa

gen starivtemp = star_iv - star  // if !=0 then rating changed again after the methodology change 
by ein: egen aaa = max(starivtemp)   
by ein: egen bbb = min(starivtemp)
gen ccc = 1 if aaa>0   // aaa>0 if your rating went down again in post-period
replace ccc = 1 if bbb<0  //bbb<0 if your rating when up again in post-period
drop aaa bbb
gen newrating_L1_iv = newrating_L1
replace newrating_L1_iv = 0 if ccc==1 // i.e. only keep neewrating =1 if if stayed at your new rating after change
drop ccc


gen three_to_four_iv=three_to_four
replace three_to_four_iv=0 if methodstarchange!=. & newrating_L1_iv==0 
replace three_to_four_iv=. if methodstarchange!=. & newrating_L1_iv==.

gen two_to_three_iv=two_to_three
replace two_to_three_iv=0 if methodstarchange!=. & newrating_L1_iv==0 
replace two_to_three_iv=. if methodstarchange!=. & newrating_L1_iv==.

gen one_to_two_iv=one_to_two
replace one_to_two_iv=0 if methodstarchange!=. & newrating_L1_iv==0 
replace one_to_two_iv=. if methodstarchange!=. & newrating_L1_iv==.

gen zero_to_one_iv=zero_to_one
replace zero_to_one_iv=0 if methodstarchange!=. & newrating_L1_iv==0 
replace zero_to_one_iv=. if methodstarchange!=. & newrating_L1_iv==.

gen four_to_three_iv=four_to_three
replace four_to_three_iv=0 if methodstarchange!=. & newrating_L1_iv==0 
replace four_to_three_iv=. if methodstarchange!=. & newrating_L1_iv==.

gen three_to_two_iv=three_to_two
replace three_to_two_iv=0 if methodstarchange!=. & newrating_L1_iv==0 
replace three_to_two_iv=. if methodstarchange!=. & newrating_L1_iv==.

gen two_to_one_iv=two_to_one
replace two_to_one_iv=0 if methodstarchange!=. & newrating_L1_iv==0 
replace two_to_one_iv=. if methodstarchange!=. & newrating_L1_iv==.

gen one_to_zero_iv=one_to_zero
replace one_to_zero_iv=0 if methodstarchange!=. & newrating_L1_iv==0 
replace one_to_zero_iv=. if methodstarchange!=. & newrating_L1_iv==.


eststo iv_reg: xtivreg ln_totalcontributions_deflate (three_to_four two_to_three one_to_two zero_to_one four_to_three three_to_two two_to_one one_to_zero = three_to_four_iv-one_to_zero_iv) fundraisingexp_L1_deflated netassets_L1_deflated L1_fundraisingefficiency CN2_1  i.modate if PublicationYear>=2015 & PublicationYear<=2018 & methodstarchange!=., fe small vce(cluster ein)

gen donationsregsample_iv = e(sample)
sum totalnetassetsdef_mill if donationsregsample_iv==1, det // 10th percentile = 0.707822; 90th = 46.25244; 50% = 5.360655 

eststo iv_reg_small: xtivreg ln_totalcontributions_deflate (three_to_four two_to_three one_to_two zero_to_one four_to_three three_to_two two_to_one one_to_zero = three_to_four_iv-one_to_zero_iv) fundraisingexp_L1_deflated netassets_L1_deflated L1_fundraisingefficiency CN2_1  i.modate if PublicationYear>=2015 & PublicationYear<=2018 & methodstarchange!=. & totalnetassetsdef_mill<5.360655, fe small vce(cluster ein)

eststo iv_reg_large: xtivreg ln_totalcontributions_deflate (three_to_four two_to_three one_to_two zero_to_one four_to_three three_to_two two_to_one one_to_zero = three_to_four_iv-one_to_zero_iv) fundraisingexp_L1_deflated netassets_L1_deflated L1_fundraisingefficiency CN2_1  i.modate if PublicationYear>=2015 & PublicationYear<=2018 & methodstarchange!=. & totalnetassetsdef_mill>5.360655, fe small vce(cluster ein)

 
*** LaTeX table

estadd local charity_fixed "Yes", replace : *
estadd local time_fixed "Yes", replace : *


esttab iv_reg iv_reg_small iv_reg_large  ///
  using "$output/donor_response_iv.tex", ///
  replace label nogap nonote ///
  se ///
  mlabels("\shortstack{All charities}" "\shortstack{Small}" "\shortstack{Large}") ///
  keep(three_to_four two_to_three one_to_two zero_to_one four_to_three three_to_two two_to_one one_to_zero) ///
  order(three_to_four two_to_three one_to_two zero_to_one four_to_three three_to_two two_to_one one_to_zero) ///
  varlabels( ///
            three_to_four "Rating increased from 3 to 4 stars" ///
            two_to_three "Rating increased from 2 to 3 stars" ///
            one_to_two "Rating increased from 1 to 2 stars" ///
            zero_to_one "Rating increased from 0 to 1 star" ///
            four_to_three "Rating decreased from 4 to 3 stars" ///
            three_to_two "Rating decreased from 3 to 2 stars" ///
            two_to_one "Rating decreased from 2 to 1 star" ///
            one_to_zero "Rating decreased from 1 to 0 stars" ///
            ) ///
  nonumbers ///
  eqlabels(none) ///
  collabels(none) ///
  star(* 0.1 ** 0.05 *** 0.01) ///
  legend ///
  addnotes("Notes: The dependent variable is log(contributions)" "Regressions control for fundraising expenses, net assets, and fundraising efficiency - all lagged by one period" "Small (large) charities are those with net assets less (greater) than $5.4 million (50th percentile)" "Standard errors (in parentheses) clustered at the charity level") ///
  stats(charity_fixed time_fixed N r2, fmt(%9.0g) ///
        labels("Charity Fixed Effects" ///
               "Time Fixed Effects" ///
				"Observations" ///
               "R-Squared" ///
               ) ///
       ) ///
  title("Effect on Donations of a Change in Star Rating - IV reduced form")
  


********************************************
** TABLE C.6: IV reduced form (2) 2015-18 **
********************************************
***** ALTERNATIVE IV STRATEGY: Calculate charity's score using old method (i.e. current year financials for non-capacity and no liabilities to assets). Use that as an instrument for new 

gen progexpenseratioCN20 = programexpenses/totalexpenses
gen fundexpratioCN20 = fundraisingexpenses/totalexpenses
gen adminexpratioCN20 = administrativeexpenses/totalexpenses
gen fundeffCN20 = fundraisingexpenses/totalcontributions


** Program expense percentage - higher is better **
gen ProgExpRatio_convert_CN20 = .
label var ProgExpRatio_convert_CN20 "Program Expense Ratio converted score (using CN 2.0 in post period)"

// 0 points if program expenses<0.5, regardless of sector
replace ProgExpRatio_convert_CN20 = 0 if progexpenseratioCN20<0.5 & progexpenseratioCN20>=0.33 

// Other
replace ProgExpRatio_convert_CN20 = 10 if (causeid ==1 | causeid==2 | causeid==4 | causeid>5 & causeid<18 | causeid==19 | causeid==20 | causeid==21 | causeid>22 & causeid<30 | causeid>30) & progexpenseratioCN20>0.85 
replace ProgExpRatio_convert_CN20 = 10*(progexpenseratioCN20-0.5)/0.35  if (causeid ==1 | causeid==2 | causeid==4 | causeid>5 & causeid<18 | causeid==19 | causeid==20 | causeid==21 | causeid>22 & causeid<30 | causeid>30) & progexpenseratioCN20<=0.85 & progexpenseratioCN20>=0.5 

// Community Foundations, Food and Humanitarian Relief
replace ProgExpRatio_convert_CN20 = 10 if (causeid == 22 | causeid==18 | causeid==30) & progexpenseratioCN20>0.92 
replace ProgExpRatio_convert_CN20 = 10*(progexpenseratioCN20-0.5)/0.42  if (causeid == 22 | causeid==18 | causeid==30) & progexpenseratioCN20<=0.92 & progexpenseratioCN20>=0.5 

// Museums
replace ProgExpRatio_convert_CN20 = 10 if causeid == 3 & progexpenseratioCN20>0.83 
replace ProgExpRatio_convert_CN20 = 10*(progexpenseratioCN20-0.5)/0.33  if causeid == 3  & progexpenseratioCN20<=0.83 & progexpenseratioCN20>=0.5 

// Public Broadcasting and Media
replace ProgExpRatio_convert_CN20 = 10 if causeid == 5 & progexpenseratioCN20>0.82 
replace ProgExpRatio_convert_CN20 = 10*(progexpenseratioCN20-0.5)/0.32  if causeid == 5 & progexpenseratioCN20<=0.82 & progexpenseratioCN20>=0.5 

replace ProgExpRatio_convert_CN20=. if progexpenseratioCN20==.


** Fundraising expense percentage - lower is better **

gen FundExpRatio_convert_CN20 = .
label var FundExpRatio_convert_CN20 "Fundraising Expenses Ratio converted score (using CN 2.0 in post period)"

// Other
replace FundExpRatio_convert_CN20 = 10 if (causeid < 5 | causeid>5 & causeid<18 | causeid==19 | causeid==20 | causeid==21 | causeid>22 & causeid<30 | causeid>30) & fundexpratioCN20<=0.10 
replace FundExpRatio_convert_CN20 = 7.5  if (causeid < 5 | causeid>5 & causeid<18 | causeid==19 | causeid==20 | causeid==21 | causeid>22 & causeid<30 | causeid>30) & fundexpratioCN20<=0.15 & fundexpratioCN20>0.10 
replace FundExpRatio_convert_CN20 = 5  if (causeid < 5 | causeid>5 & causeid<18 | causeid==19 | causeid==20 | causeid==21 | causeid>22 & causeid<30 | causeid>30) & fundexpratioCN20<=0.20 & fundexpratioCN20>0.15 
replace FundExpRatio_convert_CN20 = 2.5  if (causeid < 5 | causeid>5 & causeid<18 | causeid==19 | causeid==20 | causeid==21 | causeid>22 & causeid<30 | causeid>30) & fundexpratioCN20<=0.25 & fundexpratioCN20>0.20 
replace FundExpRatio_convert_CN20 = 0  if (causeid < 5 | causeid>5 & causeid<18 | causeid==19 | causeid==20 | causeid==21 | causeid>22 & causeid<30 | causeid>30) & fundexpratioCN20>0.25

// Public Broadcasting and Media
replace FundExpRatio_convert_CN20 = 10 if causeid == 5 & fundexpratioCN20<=0.20 
replace FundExpRatio_convert_CN20 = 7.5  if causeid == 5 & fundexpratioCN20<=0.25 & fundexpratioCN20>0.20 
replace FundExpRatio_convert_CN20 = 5  if causeid == 5 & fundexpratioCN20<=0.30 & fundexpratioCN20>0.25 
replace FundExpRatio_convert_CN20 = 2.5  if causeid == 5 & fundexpratioCN20<=35 & fundexpratioCN20>0.30
replace FundExpRatio_convert_CN20 = 0  if causeid == 5 & fundexpratioCN20>0.35 

// Community Foundations, Food and Humanitarian Relief
replace FundExpRatio_convert_CN20 = 10 if (causeid == 22 | causeid==18 | causeid==30) & fundexpratioCN20<=0.05 
replace FundExpRatio_convert_CN20 = 7.5  if (causeid == 22 | causeid==18 | causeid==30) & fundexpratioCN20<=0.10 & fundexpratioCN20>0.05
replace FundExpRatio_convert_CN20 = 5  if (causeid == 22 | causeid==18 | causeid==30) & fundexpratioCN20<=0.15 & fundexpratioCN20>0.10
replace FundExpRatio_convert_CN20 = 2.5  if (causeid == 22 | causeid==18 | causeid==30) & fundexpratioCN20<=0.20 & fundexpratioCN20>0.15 
replace FundExpRatio_convert_CN20 = 0  if (causeid == 22 | causeid==18 | causeid==30) & fundexpratioCN20>0.20 

replace FundExpRatio_convert_CN20=. if fundexpratioCN20==.


** Fundraising efficiency - lower better **

gen FundEff_convert_CN20 = .
label var FundEff_convert_CN20 "Fundraising Efficiency converted score (using CN 2.0 in post period)"

// Other
replace FundEff_convert_CN20 = 10 if (causeid < 5 | causeid>5 & causeid<18 | causeid==19 | causeid==20 | causeid==21 | causeid>22 & causeid<30 | causeid>30) & fundeffCN20<=0.10 
replace FundEff_convert_CN20 = 7.5  if (causeid < 5 | causeid>5 & causeid<18 | causeid==19 | causeid==20 | causeid==21 | causeid>22 & causeid<30 | causeid>30) & fundeffCN20<=0.25 & fundeffCN20>0.10 
replace FundEff_convert_CN20 = 5  if (causeid < 5 | causeid>5 & causeid<18 | causeid==19 | causeid==20 | causeid==21 | causeid>22 & causeid<30 | causeid>30) & fundeffCN20<=0.35 & fundeffCN20>0.25 
replace FundEff_convert_CN20 = 2.5  if (causeid < 5 | causeid>5 & causeid<18 | causeid==19 | causeid==20 | causeid==21 | causeid>22 & causeid<30 | causeid>30) & fundeffCN20<=0.5 & fundeffCN20>0.35 
replace FundEff_convert_CN20 = 0  if (causeid < 5 | causeid>5 & causeid<18 | causeid==19 | causeid==20 | causeid==21 | causeid>22 & causeid<30 | causeid>30) & fundeffCN20>0.5 

// Public Broadcasting and Media
replace FundEff_convert_CN20 = 10 if causeid == 5 & fundeffCN20<=0.20 
replace FundEff_convert_CN20 = 7.5  if causeid == 5 & fundeffCN20<=0.3 & fundeffCN20>0.20 
replace FundEff_convert_CN20 = 5  if causeid == 5 & fundeffCN20<=0.45 & fundeffCN20>0.3 
replace FundEff_convert_CN20 = 2.5  if causeid == 5 & fundeffCN20<=1 & fundeffCN20>0.45 
replace FundEff_convert_CN20 = 0  if causeid == 5 & fundeffCN20>1 

// Community Foundations, Food and Humanitarian Relief
replace FundEff_convert_CN20 = 10 if (causeid == 22 | causeid==18 | causeid==30) & fundeffCN20<=0.03 
replace FundEff_convert_CN20 = 7.5  if (causeid == 22 | causeid==18 | causeid==30) & fundeffCN20<=0.10 & fundeffCN20>0.03 
replace FundEff_convert_CN20 = 5  if (causeid == 22 | causeid==18 | causeid==30) & fundeffCN20<=0.15 & fundeffCN20>0.10 
replace FundEff_convert_CN20 = 2.5  if (causeid == 22 | causeid==18 | causeid==30) & fundeffCN20<=0.20 & fundeffCN20>0.15 
replace FundEff_convert_CN20 = 0  if (causeid == 22 | causeid==18 | causeid==30) & fundeffCN20>0.20 

replace FundEff_convert_CN20=. if fundeffCN20==.


** Admin ratio - lower better **
gen AdminExpRatio_convert_CN20 = .
label var AdminExpRatio_convert_CN20 "Admin Expense Ratio converted score (using CN 2.0 in post period)"

// Other
replace AdminExpRatio_convert_CN20 = 10 if (causeid ==1 | causeid==2 | causeid>3 & causeid<18 | causeid==19 | causeid==20 | causeid==21 | causeid>22 & causeid<30| causeid>30) & adminexpratioCN20<=0.15
replace AdminExpRatio_convert_CN20 = 7.5  if (causeid ==1 | causeid==2 | causeid>3 & causeid<18 | causeid==19 | causeid==20 | causeid==21 | causeid>22 & causeid<30| causeid>30) & adminexpratioCN20<=0.2 & adminexpratioCN20>0.15
replace AdminExpRatio_convert_CN20 = 5  if (causeid ==1 | causeid==2 | causeid>3 & causeid<18 | causeid==19 | causeid==20 | causeid==21 | causeid>22 & causeid<30| causeid>30) & adminexpratioCN20<=0.25 & adminexpratioCN20>0.2
replace AdminExpRatio_convert_CN20 = 2.5  if (causeid ==1 | causeid==2 | causeid>3 & causeid<18 | causeid==19 | causeid==20 | causeid==21 | causeid>22 & causeid<30| causeid>30) & adminexpratioCN20<=0.3 & adminexpratioCN20>0.25
replace AdminExpRatio_convert_CN20 = 0  if (causeid ==1 | causeid==2 | causeid>3 & causeid<18 | causeid==19 | causeid==20 | causeid==21 | causeid>22 & causeid<30| causeid>30) & adminexpratioCN20>0.3

// Food and Humanitarian Relief
replace AdminExpRatio_convert_CN20 = 10 if (causeid == 18 | causeid==30) & adminexpratioCN20<=0.03
replace AdminExpRatio_convert_CN20 = 7.5  if (causeid == 18 | causeid==30) & adminexpratioCN20<=0.05 & adminexpratioCN20>0.03
replace AdminExpRatio_convert_CN20 = 5  if (causeid == 18 | causeid==30) & adminexpratioCN20<=0.10 & adminexpratioCN20>0.05
replace AdminExpRatio_convert_CN20 = 2.5  if (causeid == 18 | causeid==30) & adminexpratioCN20<=0.15 & adminexpratioCN20>0.10
replace AdminExpRatio_convert_CN20 = 0  if (causeid == 18 | causeid==30) & adminexpratioCN20>0.15


// Community Foundations
replace AdminExpRatio_convert_CN20 = 10 if causeid == 22 & adminexpratioCN20<=0.10
replace AdminExpRatio_convert_CN20 = 7.5  if causeid == 22 & adminexpratioCN20<=0.15 & adminexpratioCN20>0.10
replace AdminExpRatio_convert_CN20 = 5  if causeid == 22 & adminexpratioCN20<=0.20 & adminexpratioCN20>0.15
replace AdminExpRatio_convert_CN20 = 2.5  if causeid == 22 & adminexpratioCN20<=0.25 & adminexpratioCN20>0.20
replace AdminExpRatio_convert_CN20 = 0  if causeid == 22 & adminexpratioCN20>0.25

// Museums
replace AdminExpRatio_convert_CN20 = 10 if causeid == 3 & adminexpratioCN20<=0.175
replace AdminExpRatio_convert_CN20 = 7.5  if causeid == 3 & adminexpratioCN20<=0.25 & adminexpratioCN20>0.175
replace AdminExpRatio_convert_CN20 = 5  if causeid == 3 & adminexpratioCN20<=0.30 & adminexpratioCN20>0.25
replace AdminExpRatio_convert_CN20 = 2.5  if causeid == 3 & adminexpratioCN20<=0.45 & adminexpratioCN20>0.30
replace AdminExpRatio_convert_CN20 = 0  if causeid == 3 & adminexpratioCN20>0.45

replace AdminExpRatio_convert_CN20=. if adminexpratioCN20==.


** Prog exp growth: [ ( Yn / Y0 ) ^ ( 1 / n ) ]  - 1, where n=4
** Prog service rev growth convert
gen progexpgrowth_convert_CN20 = programexpensesgrowth
label var progexpgrowth_convert_CN20 "Program expenses growth converted score (using CN 2.0 in post period)"

replace progexpgrowth_convert_CN20 = programexpensesgrowth + 1 if (causeid==13 | causeid==20 | causeid==39 | causeid==24 | causeid==34 | causeid==16 | causeid==32 | causeid==40) 
replace progexpgrowth_convert_CN20 = programexpensesgrowth + 2 if (causeid==18 | causeid==43 | causeid==6 | causeid==3 | causeid==14 | causeid==15 | causeid==38 | causeid==44) 
replace progexpgrowth_convert_CN20 = programexpensesgrowth + 3 if (causeid==19 | causeid==35 | causeid==4 | causeid==5 | causeid==26)
replace progexpgrowth_convert_CN20 = programexpensesgrowth + 4 if (causeid==41 | causeid==25) 
replace progexpgrowth_convert_CN20 = programexpensesgrowth + 6 if causeid==30 
replace progexpgrowth_convert_CN20 = programexpensesgrowth + 7 if causeid==42 

replace progexpgrowth_convert_CN20=10 if (progexpgrowth_convert_CN20!=. & progexpgrowth_convert_CN20>10)
replace progexpgrowth_convert_CN20=0 if (progexpgrowth_convert_CN20!=. & progexpgrowth_convert_CN20<0)


* Working capital ratio
gen WorkingCap_convert_CN20 = .
label var WorkingCap_convert_CN20 "Working Capital Ratio converted score (using CN 2.0 in post period)"

// Other
replace WorkingCap_convert_CN20 = 10 if (causeid ==1 | causeid==2 | causeid==4 | causeid==5 | causeid>6 & causeid<10 | causeid>10 & causeid<18 | causeid==19 | causeid==21 | causeid==22 | causeid==23 | causeid==24 | causeid==27 | causeid==28 | causeid==29 | causeid>30)  & workingcapitalratio>1
replace WorkingCap_convert_CN20 = 7.5  if (causeid ==1 | causeid==2 | causeid==4 | causeid==5 | causeid>6 & causeid<10 | causeid>10 & causeid<18 | causeid==19 | causeid==21 | causeid==22 | causeid==23 | causeid==24 | causeid==27 | causeid==28 | causeid==29 | causeid>30) & workingcapitalratio>0.5 & workingcapitalratio<=1
replace WorkingCap_convert_CN20 = 5  if (causeid ==1 | causeid==2 | causeid==4 | causeid==5 | causeid>6 & causeid<10 | causeid>10 & causeid<18 | causeid==19 | causeid==21 | causeid==22 | causeid==23 | causeid==24 | causeid==27 | causeid==28 | causeid==29 | causeid>30) & workingcapitalratio>0.25 & workingcapitalratio<=0.5
replace WorkingCap_convert_CN20 = 2.5  if (causeid ==1 | causeid==2 | causeid==4 | causeid==5 | causeid>6 & causeid<10 | causeid>10 & causeid<18 | causeid==19 | causeid==21 | causeid==22 | causeid==23 | causeid==24 | causeid==27 | causeid==28 | causeid==29 | causeid>30) & workingcapitalratio>0 & workingcapitalratio<=0.25
replace WorkingCap_convert_CN20 = 0  if (causeid ==1 | causeid==2 | causeid==4 | causeid==5 | causeid>6 & causeid<10 | causeid>10 & causeid<18 | causeid==19 | causeid==21 | causeid==22 | causeid==23 | causeid==24 | causeid==27 | causeid==28 | causeid==29 | causeid>30) & workingcapitalratio<=0

// Food
replace WorkingCap_convert_CN20 = 10 if causeid == 18 & workingcapitalratio>0.25
replace WorkingCap_convert_CN20 = 7.5  if causeid == 18 & workingcapitalratio>0.0833 & workingcapitalratio<=0.25
replace WorkingCap_convert_CN20 = 5  if causeid == 18 & workingcapitalratio>0.042 & workingcapitalratio<=0.0833
replace WorkingCap_convert_CN20 = 2.5  if causeid == 18 & workingcapitalratio>0 & workingcapitalratio<=0.042
replace WorkingCap_convert_CN20 = 0  if causeid == 18 & workingcapitalratio<=0

// Development and Relief, Religious Activities, and Religious Media and Broadcasting
replace WorkingCap_convert_CN20 = 10 if (causeid == 20 | causeid==25 | causeid==26) & workingcapitalratio>1
replace WorkingCap_convert_CN20 = 7.5  if (causeid == 20 | causeid==25 | causeid==26) & workingcapitalratio>0.5 & workingcapitalratio<=1
replace WorkingCap_convert_CN20 = 5  if (causeid == 20 | causeid==25 | causeid==26) & workingcapitalratio>0.0833 & workingcapitalratio<=0.5
replace WorkingCap_convert_CN20 = 2.5  if (causeid == 20 | causeid==25 | causeid==26) & workingcapitalratio>0 & workingcapitalratio>0.0833
replace WorkingCap_convert_CN20 = 0  if (causeid == 20 | causeid==25 | causeid==26) & workingcapitalratio<=0

//Humanitarian Relief
replace WorkingCap_convert_CN20 = 10 if causeid == 30 & workingcapitalratio>0.1
replace WorkingCap_convert_CN20 = 7.5  if causeid == 30 & workingcapitalratio>0.06 & workingcapitalratio<=0.10
replace WorkingCap_convert_CN20 = 5  if causeid == 30 & workingcapitalratio>0.02 & workingcapitalratio<=0.06
replace WorkingCap_convert_CN20 = 2.5  if causeid == 30 & workingcapitalratio>0 & workingcapitalratio<=0.02
replace WorkingCap_convert_CN20 = 0  if causeid == 30 & workingcapitalratio<=0

// Parks and Museums
replace WorkingCap_convert_CN20 = 10 if (causeid == 10 | causeid==3) & workingcapitalratio>3
replace WorkingCap_convert_CN20 = 7.5  if (causeid == 10 | causeid==3) & workingcapitalratio>1 & workingcapitalratio<=3
replace WorkingCap_convert_CN20 = 5  if (causeid == 10 | causeid==3) & workingcapitalratio>0.5 & workingcapitalratio<=1
replace WorkingCap_convert_CN20 = 2.5  if (causeid == 10 | causeid==3) & workingcapitalratio>0 & workingcapitalratio<=0.5
replace WorkingCap_convert_CN20 = 0  if (causeid == 10 | causeid==3) & workingcapitalratio<=0

// Libraries
replace WorkingCap_convert_CN20 = 10 if causeid == 6 & workingcapitalratio>4
replace WorkingCap_convert_CN20 = 7.5  if causeid == 6 & workingcapitalratio>1 & workingcapitalratio<=4
replace WorkingCap_convert_CN20 = 5  if causeid == 6 & workingcapitalratio>0.5 & workingcapitalratio<=1
replace WorkingCap_convert_CN20 = 2.5  if causeid == 6 & workingcapitalratio>0 & workingcapitalratio<=0.5
replace WorkingCap_convert_CN20 = 0  if causeid == 6 & workingcapitalratio<=0

// Community Foundations
replace WorkingCap_convert_CN20 = 10 if causeid == 22 & workingcapitalratio>5
replace WorkingCap_convert_CN20 = 7.5  if causeid == 22 & workingcapitalratio>2 & workingcapitalratio<=5
replace WorkingCap_convert_CN20 = 5  if causeid == 22 & workingcapitalratio>1 & workingcapitalratio<=2
replace WorkingCap_convert_CN20 = 2.5  if causeid == 22 & workingcapitalratio>0 & workingcapitalratio<=1
replace WorkingCap_convert_CN20 = 0  if causeid == 22 & workingcapitalratio<=0

gen finscore_CN20 =  FundEff_convert_CN20 + FundExpRatio_convert_CN20 + ProgExpRatio_convert_CN20 + AdminExpRatio_convert_CN20 + progexpgrowth_convert_CN20 + WorkingCap_convert_CN20 + 40

gen score_CN20 = 100 - sqrt(((100 - finscore_CN20)^2 + (100 - accountscore)^2)/2)
replace score_CN20 = score if CN2_1==0

gen star_CN20 = star
replace star_CN20 = 0 if score_CN20<55 & CN2_1==1
replace star_CN20 = 1 if score_CN20>=55 & score_CN20<70 & CN2_1==1
replace star_CN20 = 2 if score_CN20>=70 & score_CN20<80 & CN2_1==1
replace star_CN20 = 3 if score_CN20>=80 & score_CN20<90 & CN2_1==1
replace star_CN20 = 4 if score_CN20>=90 & CN2_1==1
label var star_CN20 "Star rating for scores calculated using CN 2.0 in post period"

sort ein modate
bys ein CN2_1: gen aaa = _n  
gen staratchangeCN20 = star_CN20 if postchange==1 & aaa==1   //aaa==1 is the immediate post-period

** Binary indicators for star change
gen three_to_fourCN20 = 1 if methodstarchange!=. & (staratchangeCN20==4 & old_star==3)
replace three_to_fourCN20=0 if methodstarchange!=. & three_to_fourCN20==.
gen two_to_threeCN20 = 1 if methodstarchange!=. & (staratchangeCN20==3 & old_star==2)
replace two_to_threeCN20=0 if methodstarchange!=. & two_to_threeCN20==.
gen one_to_twoCN20 = 1 if methodstarchange!=. & (staratchangeCN20==2 & old_star==1)
replace one_to_twoCN20=0 if methodstarchange!=. & one_to_twoCN20==.
gen zero_to_oneCN20 = 1 if methodstarchange!=. & (staratchangeCN20==1 & old_star==0)
replace zero_to_oneCN20=0 if methodstarchange!=. & zero_to_oneCN20==.

gen four_to_threeCN20 = 1 if methodstarchange!=. & (staratchangeCN20==3 & old_star==4)
replace four_to_threeCN20=0 if methodstarchange!=. & four_to_threeCN20==.
gen three_to_twoCN20 = 1 if methodstarchange!=. & (staratchangeCN20==2 & old_star==3)
replace three_to_twoCN20=0 if methodstarchange!=. & three_to_twoCN20==.
gen two_to_oneCN20 = 1 if methodstarchange!=. & (staratchangeCN20==1 & old_star==2)
replace two_to_oneCN20=0 if methodstarchange!=. & two_to_oneCN20==.
gen one_to_zeroCN20 = 1 if methodstarchange!=. & (staratchangeCN20==0 & old_star==1)
replace one_to_zeroCN20=0 if methodstarchange!=. & one_to_zeroCN20==.


*** Regs:

eststo iv_reg: xtivreg ln_totalcontributions_deflate (three_to_four two_to_three one_to_two zero_to_one four_to_three three_to_two two_to_one one_to_zero = three_to_fourCN20-one_to_zeroCN20) fundraisingexp_L1_deflated netassets_L1_deflated L1_fundraisingefficiency CN2_1  i.modate if PublicationYear>=2015 & PublicationYear<=2018 & methodstarchange!=., fe small vce(cluster ein)

gen donationsregsample_ivCN20 = e(sample)
sum totalnetassetsdef_mill if donationsregsample_ivCN20==1, det // 10th percentile = 0.707822; 90th = 46.25244; 50% = 5.360655 


eststo iv_reg_small: xtivreg ln_totalcontributions_deflate (three_to_four two_to_three one_to_two zero_to_one four_to_three three_to_two two_to_one one_to_zero = three_to_fourCN20-one_to_zeroCN20) fundraisingexp_L1_deflated netassets_L1_deflated L1_fundraisingefficiency CN2_1  i.modate if PublicationYear>=2015 & PublicationYear<=2018 & methodstarchange!=. & totalnetassetsdef_mill<5.360655, fe small vce(cluster ein)


eststo iv_reg_large: xtivreg ln_totalcontributions_deflate (three_to_four two_to_three one_to_two zero_to_one four_to_three three_to_two two_to_one one_to_zero = three_to_fourCN20-one_to_zeroCN20) fundraisingexp_L1_deflated netassets_L1_deflated L1_fundraisingefficiency CN2_1  i.modate if PublicationYear>=2015 & PublicationYear<=2018 & methodstarchange!=. & totalnetassetsdef_mill>5.360655, fe small vce(cluster ein)


*** LaTeX table

estadd local charity_fixed "Yes", replace : *
estadd local time_fixed "Yes", replace : *


esttab iv_reg iv_reg_small iv_reg_large  ///
  using "$output/donor_response_iv2.tex", ///
  replace label nogap nonote ///
  se ///
  mlabels("\shortstack{All charities}" "\shortstack{Small}" "\shortstack{Large}") ///
  keep(three_to_four two_to_three one_to_two zero_to_one four_to_three three_to_two two_to_one one_to_zero) ///
  order(three_to_four two_to_three one_to_two zero_to_one four_to_three three_to_two two_to_one one_to_zero) ///
  varlabels( ///
            three_to_four "Rating increased from 3 to 4 stars" ///
            two_to_three "Rating increased from 2 to 3 stars" ///
            one_to_two "Rating increased from 1 to 2 stars" ///
            zero_to_one "Rating increased from 0 to 1 star" ///
            four_to_three "Rating decreased from 4 to 3 stars" ///
            three_to_two "Rating decreased from 3 to 2 stars" ///
            two_to_one "Rating decreased from 2 to 1 star" ///
            one_to_zero "Rating decreased from 1 to 0 stars" ///
            ) ///
  nonumbers ///
  eqlabels(none) ///
  collabels(none) ///
  star(* 0.1 ** 0.05 *** 0.01) ///
  legend ///
  addnotes("Notes: The dependent variable is log(contributions)" "Regressions control for fundraising expenses, net assets, and fundraising efficiency - all lagged by one period" "Small (large) charities are those with net assets less (greater) than $5.4 million (50th percentile)" "Standard errors (in parentheses) clustered at the charity level") ///
  stats(charity_fixed time_fixed N r2, fmt(%9.0g) ///
        labels("Charity Fixed Effects" ///
               "Time Fixed Effects" ///
				"Observations" ///
               "R-Squared" ///
               ) ///
       ) ///
  title("Effect on Donations of a Change in Star Rating - IV reduced form")
  

